<?php

class SON_Forms_Partido extends Zend_Form
{
    public function init()
    {
    	$fc = Zend_Controller_Front::getInstance();
    	$request = $fc->getRequest();
    	
    	$this->setMethod('post');
        
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');
        $this->addElement($id);
        
        $validate = new Zend_Validate_Db_NoRecordExists(array('table' => 'partido', 'field' => 'numero'));
        $validate->getSelect()->where('sigla = ?', $request->getParam('sigla'));
        $validate->setMessage('Número já cadastrado');
        
        $numero = new Zend_Form_Element_Text('numero');
        $numero->setRequired(true)
	        ->addFilter('Digits')
	        ->addFilter('StripTags')
	        ->addFilter('StringTrim')
	        ->addValidator('NotEmpty', true, array('messages' => 'Informe um número'))
	        ->addValidator($validate, true)
	        ->setAttrib('class', 'form-control')
	        ->setAttrib('placeholder', 'Número')
        	->setAttrib('required', 'required');
        $this->addElement($numero);
        
        $validate = new Zend_Validate_Db_NoRecordExists(array('table' => 'partido', 'field' => 'sigla'));
        $validate->getSelect()->where('numero = ?', $request->getParam('numero'));
        $validate->setMessage('Sigla já cadastrada');
        
        $sigla = new Zend_Form_Element_Text('sigla');
        $sigla->setRequired(true)
	        ->addFilter('StripTags')
	        ->addFilter('StringTrim')
	        ->addValidator('NotEmpty', true, array('messages' => 'Informe uma sigla'))
	        ->addValidator($validate, true)
	        ->setAttrib('class', 'form-control')
	        ->setAttrib('placeholder', 'Sigla')
        	->setAttrib('required', 'required');
        $this->addElement($sigla);
    
        $nome = new Zend_Form_Element_Text('nome');
        $nome->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true, array('messages' => 'Informe um nome'))
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'Nome')
        	->setAttrib('required', 'required');
        $this->addElement($nome);
        
        $eleicao = new Eleicao();
        $options = $eleicao->fetchPairs();
        $eleicao = new Zend_Form_Element_MultiSelect('eleicao');
        $eleicao->setAttrib('class', 'form-control')
        	->addMultiOptions($options);
        $this->addElement($eleicao);
    
        $id->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $numero->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $sigla->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $nome->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $eleicao->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
    }
    
    public function setAsEditForm(array $data)
    {
        $this->populate($data);
        
        $this->getElement('numero')
        	->getValidator('Db_NoRecordExists')
        	->getSelect()->where('id <> ?', (int) $data['id']);
        
        $this->getElement('sigla')
        	->getValidator('Db_NoRecordExists')
        	->getSelect()->where('id <> ?', (int) $data['id']);
        
        return $this;
    }
}